// import { getToken } from '@/utils/tokenStorage.js'

export const baseURL = 'http://127.0.0.1:8090'

/**
 * 封装request方法
 */
export function request(config = {}) {

	let {
		url,
		method = 'GET',
		header = {},
		data = {}
	} = config
	url = baseURL + url
	// const token = getToken()
	// if (token && token !== '') {
	// 	header['authorization'] = token
	// }

	if ((method.toUpperCase() === 'PUT' || method.toUpperCase() === 'POST') && !header['content-type']) {
		header['content-type'] = 'application/json;charset=UTF-8'
	}

	return new Promise((resolve, reject) => {
		uni.request({
			url,
			method,
			header,
			data,
			success: (res) => {
				if (res.statusCode === 200) {
					// statusCode 200 代表服务器响应正常，但业务不一定正常
					const data = res.data
					if (data.code === 200) {
						// code 200 代表业务正常
						resolve(data)
					} else {
						uni.showToast({
							icon: 'none',
							title: data.msg
						})
						reject(data)
					}
				} else {
					uni.showToast({
						icon: 'none',
						title: res.message
					})
					reject(res.data)
				}
			},
			fail: err => {
				reject(err)
			}
		})
	})

}